﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MyOA.Model;


namespace MyOA.DAL
{
   public class AllScheduleService
    {
       DBHelper db = new DBHelper();


       /// <summary>
       /// 根据用户ID 获取这用户所有的个人日程
       /// </summary>
       /// <param name="userId">用户ID</param>
       /// <param name="size">一页显示多少条数据</param>
       /// <param name="currentPage">当前是第几页</param>
       /// <param name="isCount">是否要进行计算总行数的操作0是进行，因为这个操作只进行一次</param>
       /// <param name="sum">out所有日程的总数</param>
       /// <returns>获取这用户所有的个人日程</returns>
       public IList<Schedule> getAllScheduleByUserId(int userId,int size,int currentPage,int isCount,out int sum)
       {
           sum = 0;
           IList<Schedule> schedules = new List<Schedule>();
           SqlCommand command = new SqlCommand("sp_getSchedulesByUserId", db.Connection);
           command.CommandType = CommandType.StoredProcedure;

           SqlParameter[] value = new SqlParameter[]
           {
               new SqlParameter("@size",size),
               new SqlParameter("@currentPage",currentPage),
               new SqlParameter("@createUserId",userId),
               new SqlParameter("@isCount",isCount),
               new SqlParameter("@sum",sum)
           };
           command.Parameters.AddRange(value);
           command.Parameters["@sum"].Direction = ParameterDirection.Output;

           using (SqlDataReader reader= command.ExecuteReader())
           {
               while (reader.Read())
               {
                   Schedule schedule = new Schedule();
                   schedule.ScheduleId = (int)reader[0];
                   schedule.Title = (string)reader[1];
                   schedule.Address = (string)reader[2];
                   schedule.BeginTime = (DateTime)reader[3];
                   schedule.EndTime = (DateTime)reader[4];
                   schedule.CreateTime = (DateTime)reader[5];
                   schedules.Add(schedule);
               }
           }
           sum = (int)command.Parameters["@sum"].Value;
           db.CloseConnection();
           return schedules;
       }

       /// <summary>
       /// 根据用户登陆名获取用户ID
       /// </summary>
       /// <param name="loginId">用户登陆名</param>
       /// <returns>用户ID</returns>
       public int getUserIdByLoginId(string loginId)
       {
           return (int)db.getScalar("sp_getUserIdByLoginId",new SqlParameter("@loginId",loginId));
       }
    }
}
